package org.ghost.web.controller;

import org.apache.commons.lang3.StringUtils;
import org.ghost.web.model.RequestModel;
import org.ghost.web.model.ResponseModel;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;

import javax.servlet.http.HttpServletRequest;
import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.StandardOpenOption;
import java.util.Map;

/**
 * Created by Administrator on 2017-07-17.
 */
@RestController
@RequestMapping("/user")
public class UserController {
    private static Logger logger = LoggerFactory.getLogger(UserController.class);

    @RequestMapping(value = "/getUser", method = RequestMethod.GET)
    public ResponseModel getUser(@RequestParam Integer clientSn) {
        ResponseModel response = new ResponseModel();
        try {
            if (clientSn == null) {
                response.setSuc(false);
                response.setMessage("输入参数clientSn不能为空");
            } else {
                response.setData("张三");
                response.setSuc(true);
            }
        } catch (Exception e) {
            response.setSuc(false);
            response.setMessage(e.getMessage());
            logger.error("HomeController.getUser ->" + e.getMessage());
        }

        return response;
    }

    @RequestMapping(value = "/addUser", method = RequestMethod.POST)
    public ResponseModel addUser(@RequestBody RequestModel model) {
        ResponseModel response = new ResponseModel();
        try {
            if (model == null || StringUtils.isBlank(model.getData())) {
                response.setSuc(false);
                response.setMessage("输入参数model不能为空");
            } else {
                response.setData(model);
                response.setSuc(true);
            }
        } catch (Exception e) {
            response.setSuc(false);
            response.setMessage(e.getMessage());
            logger.error("HomeController.addUser ->" + e.getMessage());
        }

        return response;
    }

    @RequestMapping(value = "/updateUser/{clientSn}", method = RequestMethod.PUT)
    public ResponseModel updateUser(@RequestBody RequestModel model, @PathVariable Integer clientSn) {
        ResponseModel response = new ResponseModel();
        try {
            if (clientSn == null) {
                response.setSuc(false);
                response.setMessage("输入参数clientSn不能为空");
            } else if (model == null || StringUtils.isBlank(model.getData())) {
                response.setSuc(false);
                response.setMessage("输入参数model不能为空");
            } else {
                response.setData(model);
                response.setSuc(true);
            }
        } catch (Exception e) {
            response.setSuc(false);
            response.setMessage(e.getMessage());
            logger.error("HomeController.updateUser ->" + e.getMessage());
        }

        return response;
    }

    @RequestMapping(value = "/deleteUser/{clientSn}", method = RequestMethod.DELETE)
    public ResponseModel deleteUser(@PathVariable Integer clientSn) {
        ResponseModel response = new ResponseModel();
        try {
            if (clientSn == null) {
                response.setSuc(false);
                response.setMessage("输入参数clientSn不能为空");
            } else {
                response.setData(clientSn);
                response.setSuc(true);
            }
        } catch (Exception e) {
            response.setSuc(false);
            response.setMessage(e.getMessage());
            logger.error("HomeController.updateUser ->" + e.getMessage());
        }

        return response;
    }

    @RequestMapping(value = "/userImage", method = RequestMethod.POST)
    public ResponseModel uploadFile(HttpServletRequest request, MultipartHttpServletRequest multiReq) throws IOException {
        ResponseModel response = new ResponseModel();
        for (Map.Entry<String, MultipartFile> entry : multiReq.getFileMap().entrySet()) {
            String name = entry.getKey();
            MultipartFile file = entry.getValue();
            try (ByteArrayInputStream fs = (ByteArrayInputStream) file.getInputStream();
                 FileOutputStream fos = new FileOutputStream(new File("D://Logs//" + file.getOriginalFilename()));) {
                byte[] buffer = new byte[1024];
                int len = 0;
                while ((len = fs.read(buffer)) != -1) {
                    fos.write(buffer, 0, len);
                }

                response.setSuc(true);
            } catch (Exception e) {
                response.setSuc(false);
                response.setMessage(e.getMessage());
            }
        }

        return response;
    }

    @RequestMapping(value = "/uploadImage", method = RequestMethod.POST)
    public ResponseModel uploadFile(@RequestBody byte[] data) throws IOException {
        ResponseModel response = new ResponseModel();
        try {
            Path path = Paths.get("D://Logs/123456.pdf");
            Path result = Files.write(path, data, StandardOpenOption.CREATE, StandardOpenOption.WRITE);

            response.setSuc(true);
            response.setData(result);
        } catch (Exception e) {
            response.setSuc(false);
            response.setMessage(e.getMessage());
            e.printStackTrace();
        }

        return response;
    }
}
